package com.truyentranh.modules.tamdiem;

import java.net.URLDecoder;
import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.truyentranh.R;
import com.truyentranh.controls.WarperImageView;
import com.truyentranh.data.TamDiemMetalData;
import com.truyentranh.utils.GlobalData;

public class TamDiemRenderAdapter extends ArrayAdapter<TamDiemMetalData> {

	ArrayList<TamDiemMetalData> blockList = null;
	public boolean useTruyenTranh = false;
	private LayoutInflater inflater;

	public TamDiemRenderAdapter(Context context, int resource, int textViewResourceId, ArrayList<TamDiemMetalData> objects) {
		super(context, resource, textViewResourceId, objects);
		this.blockList = objects;
		this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public ArrayList<TamDiemMetalData> getData(){
		return this.blockList;
	}

	public void setNewData(ArrayList<TamDiemMetalData> blockList){
		this.blockList = blockList;
		notifyDataSetChanged();
	}


	static public class ViewHolder {
		public WarperImageView imageView;
		public TextView header;
		public TextView caption;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		ViewHolder holder;
		if(convertView == null){
			holder = new ViewHolder();
			if(useTruyenTranh){
				convertView = inflater.inflate(R.layout.truyentranh_render_category, parent, false);
			} else {
				convertView = inflater.inflate(R.layout.khoanh_render_modules, parent, false);
			}
//			convertView.setPadding(2, 2, 2, 2);

			holder.header = (TextView) convertView.findViewById(R.id.nameView);
			holder.caption = (TextView) convertView.findViewById(R.id.caption);
			holder.imageView = (WarperImageView) convertView.findViewById(R.id.avatarView);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder)convertView.getTag();
		}

		TamDiemMetalData module = blockList.get(position);
		if(holder.header != null) holder.header.setText(module.getName());
		if(holder.caption != null) holder.caption.setText(module.getCaption());

		final String url = module.getImage();
		String decode = URLDecoder.decode(url);

		GlobalData.mAQ.id(holder.imageView).image(decode, GlobalData.opt_avatar);

		return convertView;
	}
}
